/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editpolicy;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import ro.pub.cs.se.moondance.command.ClassCreateCommand;
import ro.pub.cs.se.moondance.command.PackageCreateCommand;
import ro.pub.cs.se.moondance.command.SetConstraintCommand;
import ro.pub.cs.se.moondance.editpart.UMLEntityEditPart;
import ro.pub.cs.se.moondance.model.ModelElement;
import ro.pub.cs.se.moondance.model.UMLBaseClass;
import ro.pub.cs.se.moondance.model.UMLEntity;
import ro.pub.cs.se.moondance.model.UMLPackage;

public class UMLXYLayoutEditPolicy extends XYLayoutEditPolicy {

	/*
	 * (non-Javadoc)
	 * 
	 * @seeConstrainedLayoutEditPolicy#createChangeConstraintCommand(
	 * ChangeBoundsRequest, EditPart, Object)
	 */
	protected Command createChangeConstraintCommand(
			ChangeBoundsRequest request, EditPart child, Object constraint) {
		if (child instanceof UMLEntityEditPart
				&& constraint instanceof Rectangle) {
			// return a command that can move and/or resize a Shape
			return new SetConstraintCommand((UMLEntity) child.getModel(),
					request, (Rectangle) constraint);
		}
		return super.createChangeConstraintCommand(request, child, constraint);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(EditPart,
	 * Object)
	 */
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		// not used in this example
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see LayoutEditPolicy#getCreateCommand(CreateRequest)
	 */
	protected Command getCreateCommand(CreateRequest request) {
		Object newObject = request.getNewObject();
		if (newObject instanceof UMLBaseClass) {
			// return a command that can add a Shape to a UMLDiagram
			return new ClassCreateCommand((UMLBaseClass) newObject,
					(ModelElement) getHost().getModel(),
					(Rectangle) getConstraintFor(request));
		}
		if (newObject instanceof UMLPackage) {
			// return a command that can add a Shape to a UMLDiagram
			return new PackageCreateCommand((UMLPackage) newObject,
					(ModelElement) getHost().getModel(),
					(Rectangle) getConstraintFor(request));
		}
		return null;
	}

}
